package com.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.PostConstruct;

@Configuration
public class RedisConfig {

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
//        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName("106.15.238.84");
        configuration.setPassword("foobared");
        configuration.setPort(6379);
//        PropertySource propertySource = new PropertySource();
//        redisSentinelConfiguration.
//                .master("master")
//                .sentinel("106.15.238.84", 6379)
//                .setPassword("foobared");

        return new LettuceConnectionFactory(configuration);
    }

    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = redisJsonSerializer();
        redisTemplate.setConnectionFactory(redisConnectionFactory());

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //redis.key序列化器
        redisTemplate.setKeySerializer(stringRedisSerializer);
        //redis.value序列化器
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        //redis.hash.key序列化器
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        //redis.hash.value序列化器
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        //调用其他初始化逻辑
        redisTemplate.afterPropertiesSet();
        //这里设置redis事务一致
        redisTemplate.setEnableTransactionSupport(true);
        return redisTemplate;
    }
    @Bean
    public Jackson2JsonRedisSerializer redisJsonSerializer() {
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);
        return serializer;
    }

    @PostConstruct
    private void injectionMybatisRedisCache() {
        RedisCache.setRedisTemplate(redisTemplate());
    }
}
